<template>
  <a-row :class="['p-4', `${prefixCls}--box`]" type="flex" :gutter="10" style="max-height: 800px">
    <a-col :xl="6" :lg="24" :md="24" style="margin-bottom: 10px">
      <DepartLeftTree ref="leftTree" @select="onTreeSelect" />
    </a-col>
    <a-col :xl="18" :lg="24" :md="24" style="margin-bottom: 10px">
      <div style="height: 100%" class="address-book">
        <a-card :bordered="false" class="j-print-demo" title="员额基本核定情况">
          <a-tag color="success">{{xzgs}}</a-tag>
          <div v-show="xzgs=='请选择公司' ? false : true">
          <!-- <a-card :bordered="false" class="j-print-demo" title="领导班子信息" style="margin-top: 2%">
            <div class="ldbz">
              <span style="margin-right: 2%">职数:</span>
              <JDictSelectTag v-model:value="Leadinggroup" placeholder="请选择职数" dictCode="ryzs" />
            </div>
          </a-card> -->
          <a-card :bordered="false" class="j-print-demo" title="内设机构信息" style="margin-top: 2%" v-show="xzgs=='领导班子' ? false : true">
            <div class="ldbz">
              <span style="margin-right: 2%">机构数:</span>
              <a-input-number v-model:value="Internalmechanism" :min="1" :max="1000" />
              <span style="margin-left: 2%; margin-right: 2%">分/子公司总数:</span>
              <a-input-number v-model:value="Internalmechanismnumber" :min="1" :max="1000" />
              <span style="margin-left: 2%; margin-right: 2%">公司总员额数:</span>
              <a-input-number v-model:value="Leadinggroupnumber" :min="1" :max="1000" />
            </div>
          </a-card>
          <a-card :bordered="false" class="j-print-demo" title="部门信息" style="margin-top: 2%">
            <div v-for="(department, key) in departmentInfo" :key="key" class="ldbz" style="margin-top: 2%">
              <div style="margin-right: 2%">{{ department.title }}:</div>
              <div style="margin-right: 2%">职数:</div>
              <JDictSelectTag v-model:value="department.chiefPositionDeputyPositionStr" placeholder="请选择职数"
                dictCode="ryzs" />
              <span style="margin-left: 2%; margin-right: 2%">员额数:</span>
              <a-input-number v-model:value="department.member" :min="1" :max="1000" />
            </div>
          </a-card>
          <div style="text-align: right">
            <a-button type="primary" ghost style="margin-top: 2%" @click="saveData">保存</a-button>
          </div>
        </div>
        </a-card>
      </div>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
import { JDictSelectTag } from '/@/components/Form';
import { provide, ref } from 'vue';
import { useDesign } from '/@/hooks/web/useDesign';
import DepartLeftTree from './components/DepartLeftTree.vue';
import { queryDepartTreeSync, saveOrUpdateDepart } from './address.api';

const { prefixCls } = useDesign('address-list');
provide('prefixCls', prefixCls);
const departmentInfo = ref<any[]>([]);

//选择公司
const xzgs = ref('请选择公司');
//国资委id
const Gzwid = ref('');
//领导班子数据
const Leadinggroupcode = ref('');
const Leadinggroup = ref('');
const Leadinggroupnumber = ref('');
//内设机构数据
const Internalmechanismcode = ref('');
const Internalmechanism = ref('');
const Internalmechanismnumber = ref('');
//左边树数据选择逻辑
async function onTreeSelect(data) {
  // 初始化值
  Leadinggroupcode.value = '';
  Leadinggroup.value = '';
  Leadinggroupnumber.value = '';
  Internalmechanismcode.value = '';
  Internalmechanism.value = '';
  Internalmechanismnumber.value = '';
  departmentInfo.value = [];
  console.log('左侧点击数据', data.key);
  console.log('点击数据', data);
  xzgs.value = data.departName
  Gzwid.value = data.key
  Leadinggroupcode.value = data.key
  //根据条件进行查询
  const depvalues = await queryDepartTreeSync({
    pid: Gzwid.value,
  });
  // 机构数量
  Internalmechanism.value = data.internalInstitutions
  // 分/子公司数量
  Internalmechanismnumber.value = data.subsidiary
  //领导班子职数 chiefPositionDeputyPositionStr
  if (data.chiefPositionDeputyPositionStr == '0-0') {
    Leadinggroup.value = '';
  } else {
    Leadinggroup.value = data.chiefPositionDeputyPositionStr
  }
  //领导班子员额数量member
  Leadinggroupnumber.value = data.member
  for (let i = 0; i < depvalues.length; i++) {
    if (depvalues[i].orgCategory == 2 || depvalues[i].orgCategory == 3 && depvalues[i].departName != '领导班子') {
      console.log(depvalues[i].chiefPositionDeputyPositionStr);
      if (depvalues[i].chiefPositionDeputyPositionStr == '0-0') {
        depvalues[i].chiefPositionDeputyPositionStr = '';
      }
      departmentInfo.value.push(depvalues[i]);
    }
  }
  console.log('depvalues接口', depvalues);
}
//保存数据
async function saveData() {
  const manpowerdata = ref<any[]>([]);; //数据数组
  const leader = splitPosition(Leadinggroup.value);
  manpowerdata.value.push(leader);
  manpowerdata.value.push({
    id: Leadinggroupcode.value,
    internalInstitutions: Internalmechanism.value,
    subsidiary: Internalmechanismnumber.value,
  });
  console.log(manpowerdata)
  for (let i = 0; i < departmentInfo.value.length; i++) {
    const re = splitPositions(departmentInfo.value[i].chiefPositionDeputyPositionStr);
    departmentInfo.value[i].chiefPosition = re.chiefPosition;
    departmentInfo.value[i].deputyPosition = re.deputyPosition;
    manpowerdata.value.push(departmentInfo.value[i]);
  }
  const Postmanagement = await saveOrUpdateDepart(manpowerdata.value);
  console.log(Postmanagement)
}
//部门分割数据
function splitPositions(str) {
  const parts = str.split('-');
  return {
    chiefPosition: parts[0],
    deputyPosition: parts[1],
  };
}
//领导班子分割数据
function splitPosition(str) {
  const parts = str.split('-');
  return {
    chiefPosition: parts[0],
    deputyPosition: parts[1],
    id: Leadinggroupcode.value,
    member: Leadinggroupnumber.value,
  };
}
</script>

<style lang="less">
@import './index.less';

.j-print-demo .ant-card-body {
  margin: 0;
  border: 0 solid black;
  min-width: 800px;
  color: #000000 !important;
}

.ldbz {
  display: flex;
  align-items: center;
}
</style>
